package com.bogewang.pay.service.impl;

import com.bogewang.core.base.BaseApiService;
import com.bogewang.core.base.BaseResponse;
import com.bogewang.core.bean.BeanTransferUtils;
import com.bogewang.core.core.token.TokenUtils;
import com.bogewang.pay.entity.PayMentTransacDTO;
import com.bogewang.pay.entity.PaymentTransactionEntity;
import com.bogewang.pay.mapper.PaymentTransactionMapper;
import com.bogewang.pay.service.PayMentTransacInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PayMentTransacInfoServiceImpl extends BaseApiService<PayMentTransacDTO> implements PayMentTransacInfoService {

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private PaymentTransactionMapper paymentTransactionMapper;

    @Override
    public BaseResponse<PayMentTransacDTO> tokenByPayMentTransac(String token) {
        //1.验证token不能为空；
        if (StringUtils.isEmpty(token)){
            return setResultError("token参数不能为空！");
        }

        //2.使用token查询redisPaymentTransacID
        String value = tokenUtils.getToken(token);
        if (StringUtils.isEmpty(value)){
            return setResultError("该token已经失效或者过期！");
        }

        long transactionId = Long.parseLong(value);
        PaymentTransactionEntity paymentTransactionEntity = paymentTransactionMapper.selectById(transactionId);
        if (paymentTransactionEntity == null){
            return setResultError("未查询到该支付信息！");
        }

        return setResultSuccess(BeanTransferUtils.EToT(paymentTransactionEntity, PayMentTransacDTO.class));
    }
}
